package com.ja.openchat.ui.chat.group;

import com.zy.openchat.App;
import com.zy.openchat.core.model.group.GroupSendInfo;
import com.zy.openchat.core.model.user.MobileContactInfo;
import com.zy.openchat.util.ACache;
import com.zy.openchat.util.Common;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class GroupSendManager {

    private long Group_Send_Id = 1;

    public long createGroupSendId(){
        return Group_Send_Id++;
    }


    private volatile static GroupSendManager manager;

    private GroupSendManager() {
        init();
    }

    public static GroupSendManager getInstance() {
        if (manager == null) {
            synchronized (GroupSendManager.class) {
                if (manager == null) {
                    manager = new GroupSendManager();
                }
            }
        }
        return manager;
    }
    private ACache mACache;
    private final String KEY = "GroupSendInfos";
    private ArrayList<GroupSendInfo> mGroupSendInfo = new ArrayList<>();
    private void init(){
        mACache = ACache.get(App.getInstanse());
        Object object = mACache.getAsObject(KEY);
        if(object!=null&&object instanceof ArrayList<?>){
            ArrayList<GroupSendInfo> groupSendInfos = (ArrayList<GroupSendInfo>)object;
            if(!Common.isEmpty(groupSendInfos)){
                Collections.sort(groupSendInfos, new Comparator<GroupSendInfo>() {
                            @Override
                            public int compare(GroupSendInfo o1, GroupSendInfo o2) {
                                return (int)(o1.id-o2.id);
                            }
                        }
                );
                mGroupSendInfo = groupSendInfos;
                Group_Send_Id = mGroupSendInfo.get(mGroupSendInfo.size()-1).id+1;
            }
        }
    }


    public void appendGroupSendInfo(GroupSendInfo groupSendInfo){
        if(groupSendInfo==null) return;
        if(mGroupSendInfo==null){
            mGroupSendInfo = new ArrayList<>();
        }
        boolean isFindSame = false;
        if(!Common.isEmpty(mGroupSendInfo)){
            for(GroupSendInfo info:mGroupSendInfo){
                if(info.id == groupSendInfo.id){
                    isFindSame = true;
                    break;
                }
            }
        }
        if(isFindSame){
            groupSendInfo.id = createGroupSendId();
        }
        mGroupSendInfo.add(groupSendInfo);
        mACache.put(KEY,mGroupSendInfo);
    }

    public void removeGroupSendInfo(GroupSendInfo groupSendInfo){
        if(groupSendInfo==null) return;
        if(mGroupSendInfo==null){
            mGroupSendInfo = new ArrayList<>();
        }
        if(!Common.isEmpty(mGroupSendInfo)){
            for(GroupSendInfo info:mGroupSendInfo){
                if(info.id == groupSendInfo.id){
                    mGroupSendInfo.remove(info);
                    break;
                }
            }
        }
        mACache.put(KEY,mGroupSendInfo);
    }

    public ArrayList<GroupSendInfo> getGroupSendInfos(){
        return mGroupSendInfo;
    }


    public void clean(){
        mGroupSendInfo = null;
        mACache.put(KEY,new ArrayList<MobileContactInfo>());
    }

}
